<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="floating-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
        <div class="shape shape-4"></div>
        <div class="shape shape-5"></div>
      </div>
    </div>

    <!-- 登录表单卡片 -->
    <div class="login-card">
      <div class="card-header">
        <div class="logo-container">
          <div class="logo-icon">
            <svg-icon icon-class="user" />
          </div>
        </div>
        <h3 class="title">智行企业组织管理平台</h3>
        <p class="subtitle">欢迎回来，请登录您的账户</p>
      </div>

      <el-form 
        ref="loginForm" 
        :model="loginForm" 
        :rules="loginRules" 
        class="login-form" 
        auto-complete="on" 
        label-position="left"
      >
        <el-form-item prop="username">
          <div class="input-group">
            <span class="input-icon">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
              class="custom-input"
            />
          </div>
        </el-form-item>

        <el-form-item prop="password">
          <div class="input-group">
            <span class="input-icon">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              class="custom-input"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </div>
        </el-form-item>

        <el-button 
          :loading="loading" 
          type="primary" 
          class="login-button"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">立即登录</span>
          <span v-else>登录中...</span>
        </el-button>

        <!-- 额外功能区域 -->
        <div class="extra-options">
          <div class="remember-me">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          </div>
        </div>
      </el-form>

      <!-- 页脚信息 -->
      <div class="card-footer">
        <p class="copyright">© 2024 智行企业组织管理平台 版权所有</p>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { login } from '@/api/user'
import { setToken } from '@/utils/auth'

export default {
  name: 'Login',
  data() {
    // 用户名校验规则
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }

    // 密码校验规则
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码长度至少为3位'))
      } else {
        callback()
      }
    }

    // 数据模型
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      rememberMe: false,
      redirect: undefined
    }
  },

  mounted() {
    // 添加页面加载动画
    this.$nextTick(() => {
      const card = document.querySelector('.login-card')
      if (card) {
        card.classList.add('card-animate')
      }
    })
  },

  methods: {
    // 展示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 登录方法
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            userName: this.loginForm.username,
            password: this.loginForm.password
          }
          // 调用登录后端接口
          login(data).then((result) => {
            console.log(result)
            if (result.data.code === 200) {
              setToken(result.data.data)
              this.$message.success('登录成功！')
              console.log('login success')
              this.$router.push('/')
            } else {
              this.$message.error(result.data.message)
              this.loading = false
            }
          }).catch(() => {
            this.$message.error('登录失败，请检查网络连接')
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  }
}
</script>

<style lang="scss">
/* 全局样式重置 */
.login-container {
  .el-input {
    input {
      background: transparent !important;
      border: none !important;
      color: #333 !important;
      font-size: 14px;
      
      &::placeholder {
        color: #999 !important;
      }

      &:focus {
        background: transparent !important;
      }

      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px transparent inset !important;
        -webkit-text-fill-color: #333 !important;
        transition: background-color 5000s ease-in-out 0s;
      }
    }
  }

  .el-form-item {
    margin-bottom: 24px;
    
    .el-form-item__error {
      color: #f56565;
      font-size: 12px;
      margin-top: 4px;
    }
  }

  .el-button {
    border: none;
    border-radius: 12px;
    font-weight: 600;
    transition: all 0.3s ease;
  }

  .el-checkbox {
    .el-checkbox__label {
      color: #666;
      font-size: 14px;
    }
  }
}
</style>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;

  // 背景装饰
  .background-decoration {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;

    .floating-shapes {
      position: relative;
      width: 100%;
      height: 100%;

      .shape {
        position: absolute;
        background: rgba(255, 255, 255, 0.1);
        border-radius: 50%;
        animation: float 6s ease-in-out infinite;

        &.shape-1 {
          width: 80px;
          height: 80px;
          top: 10%;
          left: 10%;
          animation-delay: 0s;
        }

        &.shape-2 {
          width: 60px;
          height: 60px;
          top: 20%;
          right: 10%;
          animation-delay: 1s;
        }

        &.shape-3 {
          width: 100px;
          height: 100px;
          bottom: 20%;
          left: 15%;
          animation-delay: 2s;
        }

        &.shape-4 {
          width: 40px;
          height: 40px;
          top: 60%;
          right: 20%;
          animation-delay: 3s;
        }

        &.shape-5 {
          width: 70px;
          height: 70px;
          bottom: 10%;
          right: 30%;
          animation-delay: 4s;
        }
      }
    }
  }

  // 登录卡片
  .login-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
    width: 450px;
    max-width: 90vw;
    padding: 40px;
    position: relative;
    z-index: 2;
    transform: translateY(20px);
    opacity: 0;
    transition: all 0.6s ease;

    &.card-animate {
      transform: translateY(0);
      opacity: 1;
    }

    // 卡片头部
    .card-header {
      text-align: center;
      margin-bottom: 32px;

      .logo-container {
        margin-bottom: 16px;

        .logo-icon {
          display: inline-flex;
          align-items: center;
          justify-content: center;
          width: 80px;
          height: 80px;
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          border-radius: 50%;
          margin-bottom: 16px;
          box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);

          .svg-icon {
            font-size: 32px;
            color: white;
          }
        }
      }

      .title {
        font-size: 28px;
        font-weight: 700;
        color: #2d3748;
        margin: 0 0 8px 0;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
      }

      .subtitle {
        color: #718096;
        font-size: 14px;
        margin: 0;
      }
    }

    // 表单样式
    .login-form {
      .input-group {
        position: relative;
        background: #f7fafc;
        border: 2px solid #e2e8f0;
        border-radius: 12px;
        transition: all 0.3s ease;
        overflow: hidden;

        &:hover {
          border-color: #cbd5e0;
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        }

        &:focus-within {
          border-color: #667eea;
          box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
          background: white;
        }

        .input-icon {
          position: absolute;
          left: 16px;
          top: 50%;
          transform: translateY(-50%);
          color: #a0aec0;
          z-index: 2;
          transition: color 0.3s ease;

          .svg-icon {
            font-size: 18px;
          }
        }

        &:focus-within .input-icon {
          color: #667eea;
        }

        .custom-input {
          border: none;
          background: transparent;

          ::v-deep .el-input__inner {
            border: none;
            background: transparent;
            padding-left: 50px;
            padding-right: 50px;
            height: 50px;
            line-height: 50px;
            font-size: 14px;
            color: #2d3748;

            &:focus {
              border: none;
              box-shadow: none;
            }
          }
        }

        .show-pwd {
          position: absolute;
          right: 16px;
          top: 50%;
          transform: translateY(-50%);
          color: #a0aec0;
          cursor: pointer;
          z-index: 2;
          transition: color 0.3s ease;

          &:hover {
            color: #667eea;
          }

          .svg-icon {
            font-size: 18px;
          }
        }
      }

      .login-button {
        width: 100%;
        height: 50px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border: none;
        border-radius: 12px;
        font-size: 16px;
        font-weight: 600;
        color: white;
        margin-top: 8px;
        transition: all 0.3s ease;
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4);
        }

        &:active {
          transform: translateY(0);
        }

        &.is-loading {
          background: #a0aec0;
          transform: none;
          box-shadow: none;
        }
      }

      .extra-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 20px;
        padding-top: 20px;
        border-top: 1px solid #e2e8f0;

        .remember-me {
          .el-checkbox {
            ::v-deep .el-checkbox__label {
              color: #718096;
              font-size: 14px;
            }
          }
        }

        .forgot-password {
          a {
            color: #667eea;
            text-decoration: none;
            font-size: 14px;
            transition: color 0.3s ease;

            &:hover {
              color: #764ba2;
              text-decoration: underline;
            }
          }
        }
      }
    }

    // 卡片底部
    .card-footer {
      text-align: center;
      margin-top: 32px;
      padding-top: 20px;
      border-top: 1px solid #e2e8f0;

      .copyright {
        color: #a0aec0;
        font-size: 12px;
        margin: 0;
      }
    }
  }
}

// 动画定义
@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}

// 响应式设计
@media (max-width: 480px) {
  .login-container {
    padding: 20px;

    .login-card {
      padding: 24px;
      width: 100%;

      .card-header {
        .logo-container .logo-icon {
          width: 60px;
          height: 60px;

          .svg-icon {
            font-size: 24px;
          }
        }

        .title {
          font-size: 24px;
        }
      }
    }
  }
}
</style>
